/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package auctiongui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.JOptionPane;

import auctioncentral.AbstractUser;
import auctioncentral.AuCenStaffUser;
import auctioncentral.AuctionSystem;
import auctioncentral.AuthNonProfitUser;
import auctioncentral.BiddingUser;
import auctioncentral.Event;
import javax.swing.JFrame;
/**
 *
 * @author Harris, Andres Rosales (acr)
 * @version 03/03/2013 Harris, acr
 */
public class NewEvent extends javax.swing.JFrame implements ActionListener {

	/** The system the new event will belong to. **/
	private AuctionSystem my_system;
	
	private AbstractUser my_user;
	
	private Event my_old_event;

	/** Default Title corresponding to a bidders view **/
	private String title;
	
	private GregorianCalendar my_gregorian_calenar;
	
	/**
     * Creates a New Event popup.
	 * @param gregorianCalendar 
     */
    public NewEvent(final AuctionSystem the_system, GregorianCalendar gregorian_calendar) {
    	my_gregorian_calenar = gregorian_calendar;
    	my_user = null;
    	my_system = the_system;
    	my_old_event = null;
    	title = "New Event";
        initComponents();
        setUp(); 
    }
    
    /**
     * Creates an "edit event" popup.
     */
    public NewEvent(final AuctionSystem the_system, final AbstractUser the_user, final Event the_old_event) {
    	my_gregorian_calenar = null;
    	my_user = the_user;
    	my_system = the_system;
    	my_old_event = the_old_event;
    	
    	if (my_system.getMy_current_user() instanceof BiddingUser) {
    		title = "Event Information";
    	} else {
        	title = "Edit Event";
    	}

        initComponents();
        setUp();
    }
  
    
    /**
     * Sets up the second half of the jTextFields in this NewEvent.
     * @author acr
     */
    private void setUp() {
    	jButton1.addActionListener(this);
    	jButton2.addActionListener(new CancelListener(this));
    	if (my_old_event != null) {
    		jTextField1.setText(my_old_event.getName());
    		jTextField2.setText(my_old_event.getUser());
    		jTextField3.setText(my_old_event.getContactNumber());
    		jTextField4.setText(my_old_event.getIntakeUser());
    		String temp = "";
    		if (my_old_event.getDate().get(Calendar.MONTH) <= 10) {
    			temp += "0" + String.valueOf(my_old_event.getDate().get(Calendar.MONTH) + 1);
    		} else {
    			temp += String.valueOf(my_old_event.getDate().get(Calendar.MONTH) + 1);
    		}
    		temp += "/";
    		if (my_old_event.getDate().get(Calendar.DATE) <= 10) {
    			temp += "0" + String.valueOf(my_old_event.getDate().get(Calendar.DATE));
    		} else {
    			temp += String.valueOf(my_old_event.getDate().get(Calendar.DATE));
    		}
    		temp += "/";
    		temp += String.valueOf(my_old_event.getDate().get(Calendar.YEAR));
    		jTextField5.setText(temp);
    		jTextField6.setText(String.valueOf(my_old_event.getDate().HOUR)
    				+ ":" +String.valueOf( my_old_event.getDate().MINUTE));
    		jTextField7.setText(String.valueOf(my_old_event.getDuration()));
    		jTextField8.setText(String.valueOf(my_old_event.getEstItems()));
    		jTextArea1.setText(my_old_event.getComments());
    		repaint();
    	}
    	if (my_gregorian_calenar != null) {
    		jTextField5.setEditable(false);
    		
    		String temp = "";
    		if (my_gregorian_calenar.get(Calendar.MONTH) <= 10) {
    			temp += "0" + String.valueOf(my_gregorian_calenar.get(Calendar.MONTH) + 1);
    		} else {
    			temp += String.valueOf(my_gregorian_calenar.get(Calendar.MONTH) + 1);
    		}
    		temp += "/";
    		if (my_gregorian_calenar.get(Calendar.DATE) <= 10) {
    			temp += "0" + String.valueOf(my_gregorian_calenar.get(Calendar.DATE));
    		} else {
    			temp += String.valueOf(my_gregorian_calenar.get(Calendar.DATE));
    		}
    		temp += "/";
    		temp += String.valueOf(my_gregorian_calenar.get(Calendar.YEAR));
    		jTextField5.setText(temp);
    	}
    	if (my_old_event == null) {
    		jTextField2.setEditable(false);
    		jTextField2.setText(my_system.getMy_current_user().getName());
    	}
    	
    	if(my_system.getMy_current_user() instanceof BiddingUser || my_system.getMy_current_user() instanceof AuCenStaffUser) {
    		jTextField1.setEditable(false);
    		jTextField2.setEditable(false);
    		jTextField3.setEditable(false);
    		jTextField4.setEditable(false);
    		jTextField5.setEditable(false);
    		jTextField6.setEditable(false);
    		jTextField7.setEditable(false);
    		jTextField8.setEditable(false);
    		jTextArea1.setEditable(false);
    		jButton1.setVisible(false);
    		jButton2.setText("Back");
    		jTextPane2.setToolTipText("Enter Item ID");
    	}
    	
    		
    }
    
    class CancelListener implements ActionListener {
    	NewEvent my_nevent;
    	
    	public CancelListener(NewEvent the_nevent) {
    		my_nevent = the_nevent;
    	}
        public void actionPerformed(ActionEvent e)
        {
          my_nevent.setVisible(false);
          my_nevent.dispose();
        }
    }
    public JFrame getMe() {
    	return this;
    }
    
    class BidListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
	        java.awt.EventQueue.invokeLater(new Runnable() {
	            public void run() {
	            	if (my_system.getMy_current_user() instanceof BiddingUser) {
	            		
	            		if (jTextPane2.getText().length() >= 1 && (my_system.findItemByID(Integer.parseInt(jTextPane2.getText())) != null)) {
	            				            			
	            			//creates a bidders "NewItem" popup.
	            			new NewItem(my_system, my_old_event.getName(), my_system.findItemByID(Integer.parseInt(jTextPane2.getText())), my_system.getMy_current_user(), jTable2).setVisible(true);
	            		} else {
	            			JOptionPane.showMessageDialog(null, "Sorry, you've entered an invalid Item ID", "We're Sorry", JOptionPane.ERROR_MESSAGE);
	            		}
	            		
	            	} else {
	            		//Right now this view is the same for a nonprofit, and auction central.
	            		new NewItem(my_system, my_old_event.getName(), jTable2).setVisible(true);
	            	}
	            	
	                
	            }
	        });
			
		}
    	
    }
    
    /**
     * Returns a new Date formatted from the user's input.
     * @author acr
     * 
     * @param the_date The user's input to use to format the Date.
     * 
     * @return A new Date formatted from the user's input.
     */
	public GregorianCalendar formatDate(final String the_date, final String the_time) {
    	if (the_date != null) {
	    	int month = Integer.parseInt(the_date.substring(0, 2)) - 1;
	    	int day = Integer.parseInt(the_date.substring(3, 5));
	    	int year = Integer.parseInt(the_date.substring(6));
	    	
	    	int hour = Integer.parseInt(the_time.substring(0, 2));
	    	int minute = Integer.parseInt(the_time.substring(3)); 
	    	
	    	if (day >= 1 && day <= 31 && month >= 0 && month <= 11 && year >= 0 
	    		&& year <= 8099 && hour < 24 && hour >= 0 && minute < 59 && minute >= 0) {
	    		return new GregorianCalendar(year, month, day, hour, minute);
	    	}
    	}
    		
	    return null;
    }
    
    /**
     * Returns a new Start Time formatted from the user's input.
     * @author acr
     * 
     * @param the_time The user's input to use to format the Start Time.
     * 
     * @return A new Start Time formatted from the user's input
     */
    private int formatTime(final String the_time) {
    	if (the_time != null) {
	    	int hour = Integer.parseInt(the_time.substring(0, 2));
	    	int minute = Integer.parseInt(the_time.substring(3));
	    	if (hour < 24 && hour >= 0 && minute < 59 && minute >= 0)
	    		return (hour * 100) + minute;
    	}
    	JOptionPane.showMessageDialog(null, "Invalid Time " + the_time, "Alert", 
    								  JOptionPane.ERROR_MESSAGE);
    	return -1;
    }
    
    /**
     * Returns a new Duration formatted from the user's input.
     * @author acr
     * 
     * @param the_duration The user's input to use to format the Duration.
     * 
     * @return A new Duration formatted from the user's input.
     */
    private int formatDuration(final String the_duration) {
    	return Integer.parseInt(the_duration);
    }

    /**
     * Returns a new Estimated Number of Items formatted from the user's input.
     * @author acr
     * 
     * @param the_est The user's input to use to format the Estimated Number of Items.
     * 
     * @return A new Estimated Number of Items formatted from the user's input.
     */
    private int formatEstNumItems(final String the_est) {
    	return Integer.parseInt(the_est);
    }
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
 // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {


    	jPanel1 = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jTextField5 = new javax.swing.JTextField();
        jTextField6 = new javax.swing.JTextField();
        jTextField7 = new javax.swing.JTextField();
        jTextField8 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel9 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();
        EditButton = new javax.swing.JButton();
        AddButton = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTextPane2 = new javax.swing.JTextPane();
        BidButton = new javax.swing.JButton();
        BidButton.addActionListener(new BidListener());
        AddButton.addActionListener(new BidListener());
        EditButton.setEnabled(false);
        jTextPane1.setEditable(false);
        
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
    
        jLabel10.setText(title);

        jLabel1.setText("Name");

        jLabel3.setText("Contact Person");

        jLabel2.setText("<html>Contact Number<br>(xxx-xxx-xxxx)</html>");

        jLabel4.setText("Intake Person");

        jLabel5.setText("<html>Auction Date <br>(MM/DD/YYYY)</html>");

        jLabel6.setText("<html>Auction Start Time<br>(HH:MM)</html>");

        jLabel7.setText("<html>Auction Duration<br>In Hours</html>");

        jLabel8.setText("Anticipated Number of Items");

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jLabel9.setText("Comments");

        jButton1.setText("Submit");

        jButton2.setText("Cancel");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jLabel10)
                        .addGap(123, 123, 123))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel9)
                                    .addGap(287, 287, 287))
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                            .addGap(0, 0, Short.MAX_VALUE)
                                            .addComponent(jButton2)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                            .addComponent(jButton1))
                                        .addComponent(jScrollPane1))
                                    .addGap(17, 17, 17)))
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel1)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jTextField1))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel3))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jTextField3)
                                        .addComponent(jTextField2)))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel4)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jTextField4))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jTextField5))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jTextField6))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jTextField7))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel8)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jTextField8, javax.swing.GroupLayout.DEFAULT_SIZE, 173, Short.MAX_VALUE))))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton2)
                    .addComponent(jButton1))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        if (my_old_event != null) {
        	jTable2.setModel(new TableModel(my_old_event.getItems()));
            jScrollPane2.setViewportView(jTable2);
        }
        

        jTabbedPane1.setName("");

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jScrollPane3.setViewportView(jTextPane1);

        EditButton.setText("Edit Item");

        AddButton.setText("Add Item");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(EditButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(AddButton)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(EditButton)
                        .addComponent(AddButton))
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        if ((my_system.getMy_current_user() instanceof AuCenStaffUser || my_system.getMy_current_user() instanceof AuthNonProfitUser) && my_old_event != null) {
        	jTabbedPane1.addTab("NonProfit", jPanel2);
        }
        

        jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jScrollPane4.setViewportView(jTextPane2);

        BidButton.setText("Bid");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(BidButton, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(85, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(BidButton)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        if (my_system.getMy_current_user() instanceof BiddingUser && my_old_event != null) {
        	jTabbedPane1.addTab("Bidder", jPanel3);
        }
        

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 341, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTabbedPane1)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 432, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 16, Short.MAX_VALUE))))
        );

        pack();
    }// </editor-fold>     

    
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddButton;
    private javax.swing.JButton BidButton;
    private javax.swing.JButton EditButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField jTextField7;
    private javax.swing.JTextField jTextField8;
    private javax.swing.JTextPane jTextPane1;
    private javax.swing.JTextPane jTextPane2;
    // End of variables declaration//GEN-END:variables
    
    
    /**
     * Submit button listener.
     */
	@Override
	public void actionPerformed(ActionEvent e) {
		
		String contactNum = jTextField3.getText().replace("-", "").trim();
		Calendar newDate = formatDate(jTextField5.getText(), jTextField6.getText());
		int duration = Integer.parseInt(jTextField7.getText());
		int estNoItems = Integer.parseInt(jTextField8.getText());
		String comments = jTextArea1.getText();
		
		String name = jTextField1.getText().replace("-", "").replace(" ", "").replaceAll("\\d", "").trim() 
				+ "-" + ((int) newDate.get(Calendar.MONTH) + 1) 
				+ "-" + newDate.get(Calendar.DAY_OF_MONTH) 
				+ "-" + newDate.get(Calendar.YEAR);
		
		
		if (my_user == null && my_system.hasUser(jTextField2.getText()) && my_system.hasUser(jTextField4.getText())) {
			
			//Add new event functionality.
			if (my_system.addEvent(new Event(name, jTextField2.getText(), contactNum, jTextField4.getText(), newDate, duration, estNoItems, comments))) {
				JOptionPane.showMessageDialog(this, "Event Successfully added to database", "Success", JOptionPane.PLAIN_MESSAGE);
				((AuthNonProfitUser) my_system.getMy_current_user()).setCurrEvent(name);
			} else {
	            JOptionPane.showMessageDialog(null, "Sorry, you've either entered incorrect information or\nwe aren't currently accepting any new events", "Error Adding New Event", JOptionPane.ERROR_MESSAGE);
			}
			
			//Edit functionality
		} else {
			
			//Remove old event
			my_system.removeEvent(my_old_event);
			
			//when removing the old event, we probably need to preserve their items.
			
			if (my_system.addEvent(new Event(name, jTextField2.getText(), contactNum, jTextField4.getText(), newDate, duration, estNoItems, comments))) {
				JOptionPane.showMessageDialog(this, "Event Successfully added to database", "Success", JOptionPane.PLAIN_MESSAGE);	
				((AuthNonProfitUser) my_system.getMy_current_user()).setCurrEvent(name);
			} else {
		        JOptionPane.showMessageDialog(null, "Sorry, you've either entered incorrect information or\nwe aren't currently accepting any new events", "Error Editing", JOptionPane.ERROR_MESSAGE);
			}
		}
	}
}
